package com.tul.exercise.base.swing.components;

import com.tul.exercise.base.swing.SwingConsole;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @Description 《java编程思想》第22章 示例16：
 * <p>
 * 迷你编辑器
 **/
public class TextPane extends JFrame {
    private JButton addText = new JButton("Add Text");
    private JButton clearText = new JButton("Clear Text");
    private JTextPane tp = new JTextPane();
    private Random rand = new Random(47);
    //private static Generator sg = new RandomGenerator.String(7);

    public TextPane() {
        setLayout(new GridLayout(1, 1));
        addText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 10; i++) {
                    tp.setText(tp.getText() + "abc" + rand.nextInt() + "\n");
                }
            }
        });

        clearText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tp.setText("");
            }
        });

        add(new JScrollPane(tp));
        add(BorderLayout.SOUTH, addText);
        add(clearText);
    }

    public static void main(String[] args) {
        SwingConsole.run(new TextPane(), 475, 425);
    }
}
